/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package android.service.reasons;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.reasons.Notificacion;
import android.content.Intent;
import android.entidades.Data;
import android.entidades.DataLocal;
import android.entidades.Update;
import android.entidades.Usuario;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.sql.UsuarioPensamiento;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author payo
 */
public class ServicioReasons extends Service{

    private Timer timer = new Timer();
    private static final long time = 5000;
    private final IBinder binder = new MyBinder();
    private NotificationManager mNM;
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        servir();
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if(timer!=null)
            timer.cancel();
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    private void servir() {
        
        timer.scheduleAtFixedRate(new TimerTask()
        {
            @Override
            public void run()
            {
               if(Usuario.isUser()!=null)
               {
                    if(Update.isTime())
                    {
                        Update.updateTime();
                        UsuarioPensamiento.importar();
                        if(Data.getID()< Integer.parseInt(DataLocal.getDataLocal()))
                            notificacion();
                        else Data.resetID();
                    }
               }
            }
        },0,time);
    }
    
    public void notificacion() 
    {
        int n = Data.getID();
        Data.incrementID();
        CharSequence text = "Razón #"+(n+1);
        Notification notification = new Notification(android.reasons.R.drawable.bulleticon, text,System.currentTimeMillis());
        Intent intent = new Intent(this,Notificacion.class);
        intent.putExtra("valor", ""+n);
        PendingIntent contentIntent = PendingIntent.getActivity(ServicioReasons.this, 0,intent, PendingIntent.FLAG_CANCEL_CURRENT);
        notification.setLatestEventInfo(this, "ALERTA",text, contentIntent);
        notification.vibrate = new long[]{100, 200, 100, 500}; 
        notification.ledARGB = 0xFFff0000;
        notification.flags = Notification.FLAG_SHOW_LIGHTS;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.ledOnMS = 100; 
        notification.ledOffMS = 100;  
        mNM.notify(0, notification);
        Uri u = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), u);
        r.play();
    }
    
    
    public class MyBinder extends Binder
    {
        public ServicioReasons getService()
        {
            return ServicioReasons.this;
        }
    }
    
}
